package com.zuipin.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysUser;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.Encryption;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 系统登陆Controller
 *
 * @ClassName LoginController
 * @author wangcongyi
 * @date 2017年4月20日 下午3:22:35
 */
@RestController
@RequestMapping(value = "/sys/login")
@Api(value = "系统登陆", description = "系统登陆-wangcongyi")
public class SysLoginController {
	@Resource
	private ISysUserService sysUserService;
	
	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	@ApiOperation(value = "518-登陆验证-wangcongyi", notes = "登陆验证")
	@Valids({ @Valid(name = "account", required = true), @Valid(name = "password", required = true, minLength = 6, maxLength = 16), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query") })
	public Result<?> getUser(@ApiParam(required = true, name = "account", value = "用户账号") @RequestParam(required = true) String account,
			@ApiParam(required = true, name = "password", value = "用户密码") @RequestParam(required = true) String password) {
		Result<SysUser> result = new Result<>();
		SysUser user = sysUserService.findByAccount(account);
		if (user != null) {
			password = Encryption.md5(password);
			if (password.equals(user.getPassword())) {
				user.setPassword(null);
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("恭喜你，登陆成功！");
				result.setData(user);
			} else {
				result.setErrorCode(ErrorCode.NOT_FIND);
				result.setMessage("用户名或者密码错误！");
				result.setData(null);
			}
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("用户信息不存在或账号被冻结！");
			result.setData(null);
		}
		return result;
	}
}
